package Clients;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Observer;
import java.util.Observable;

import Catalogue.SoldBasket;
import Middle.*;
import Processing.Order;

class ViewOfDisplay implements Observer
{
	  private static final int height = 300;       // Height of window pixels
	  private static final int width = 400;       // Width  of window pixels

	  private JLabel      queueLoad  = new JLabel();
	  private Picture orderBox;
	  private ArrayList<Picture> orderBoxesInTransit = new ArrayList<Picture>();
	  private ArrayList<Picture> orderBoxesProcessing = new ArrayList<Picture>();
	  private ArrayList<Picture> orderBoxesWaiting = new ArrayList<Picture>();
	  private JLabel orderBoxNo = new JLabel();
	  private ArrayList<JLabel> orderBoxeNosInTransit = new ArrayList<JLabel>();
	  private ArrayList<JLabel> orderBoxeNosProcessing = new ArrayList<JLabel>();
	  private ArrayList<JLabel> orderBoxeNosWaiting = new ArrayList<JLabel>();
	  
	  private JLabel inTransit = new JLabel();
	  private JLabel processing = new JLabel();
	  private JLabel waiting = new JLabel();
	  
	  private Container container;
	  private int locX, locY;
	  private int previousWaitingSize, previousTransitSize, previousProcessingSize;

  public ViewOfDisplay( RootPaneContainer rpc )
  {
    // Add code to setup a graphical view of the display
	      
	    
	    container         = rpc.getContentPane();    // Content Pane
	    Container rootWindow = (Container) rpc;         // Root Window
	    container.setLayout(null);                             // No layout manager
	    rootWindow.setSize( width, height );                     // Size of Window
	    container.setBackground(Color.BLACK);

	    Font f = new Font("Arial",Font.BOLD,10);  // Font f is

	    queueLoad.setBounds( 110, 235, 270, 20 );       // Message area
		queueLoad.setText("Current queue load: 0");
		queueLoad.setForeground(Color.WHITE);
	    container.add( queueLoad );                            //  Add to canvas
	    
	    inTransit.setBounds( 0, 0, width, 10);       // Message area
		inTransit.setText("In transit:");
		inTransit.setFont(f);
		inTransit.setForeground(Color.WHITE);
	    container.add( inTransit );                            //  Add to canvas

	    processing.setBounds( 0, 80, width, 10);       // Message area
		processing.setText("Processing:");
		processing.setFont(f);
		processing.setForeground(Color.WHITE);
	    container.add( processing );                            //  Add to canvas
	    
	    waiting.setBounds( 0, 160, width, 10);       // Message area
		waiting.setText("Waiting at collection desk:");
		waiting.setFont(f);
		waiting.setForeground(Color.WHITE);
	    container.add( waiting );                            //  Add to canvas
	    

	    rootWindow.setVisible( true );                  // Make visible	  
  }
  


  public void update( Observable o, Object arg )
  {
     // Code to update the graphical display with the current
     //  state of the system
     //  Orders awaiting processing
     //  Orders being picked in the 'warehouse. 
     //  Orders awaiting collection
	  if(o instanceof ModelOfDisplay){
		  try {
			ArrayList<SoldBasket> waitingArray  = (ArrayList<SoldBasket>) ((ModelOfDisplay) o).getOrderArrays().get("Waiting");
			ArrayList<SoldBasket> processingArray  = (ArrayList<SoldBasket>) ((ModelOfDisplay) o).getOrderArrays().get("Processing");
			ArrayList<SoldBasket> inTransitArray  = (ArrayList<SoldBasket>) ((ModelOfDisplay) o).getOrderArrays().get("InTransit");
			queueLoad.setText("Current queue load: " + String.valueOf(waitingArray.size()+processingArray.size()+inTransitArray.size()));
			locX = 5;
			locY = 5;
			container.removeAll();
			container.add(queueLoad);
			container.add(inTransit);
			container.add(processing);
			container.add(waiting);
					
			
			if(previousTransitSize != inTransitArray.size()){
				orderBoxesInTransit = new ArrayList<Picture>();
				orderBoxeNosInTransit = new ArrayList<JLabel>();
				//first lane
				System.out.println("---transit---");
				locX = 5;
				locY = 10;
				for(int i=0; i<inTransitArray.size(); i++){
					orderBox = new Picture(50,50);
					orderBoxNo = new JLabel();
					container.add(orderBox);
					orderBoxesInTransit.add(orderBox);
					container.add(orderBoxNo);
					orderBoxeNosInTransit.add(orderBoxNo);
					orderBoxNo.setForeground(Color.WHITE);
					orderBoxNo.setText(String.valueOf(inTransitArray.get(i).getOrderNo()));
					orderBox.set(new ImageIcon("src/images/SmallBox/SmallBox.png"));
					
					orderBox.setLocation(locX, locY);
					orderBoxNo.setBounds(locX, locY+50, 50, 10);
					locX += 50;	
				}
			}
			if(previousProcessingSize != processingArray.size()){
				orderBoxesProcessing = new ArrayList<Picture>();
				orderBoxeNosProcessing = new ArrayList<JLabel>();
				//middle lane
				System.out.println("---processing---");
				locX = 5;
				locY = 90;
				for(int i=0; i<processingArray.size(); i++){
					orderBox = new Picture(50,50);
					orderBoxNo = new JLabel();
					container.add(orderBox);
					orderBoxesProcessing.add(orderBox);
					container.add(orderBoxNo);
					orderBoxeNosProcessing.add(orderBoxNo);
					orderBoxNo.setForeground(Color.WHITE);
					orderBoxNo.setText(String.valueOf(processingArray.get(i).getOrderNo()));
					orderBox.set(new ImageIcon("src/images/SmallBox/SmallBox.png"));
					
					orderBox.setLocation(locX, locY);
					orderBoxNo.setBounds(locX, locY+50, 50, 10);
					locX += 50;	
				}
				
			}
			if(previousWaitingSize != waitingArray.size()){
				orderBoxesWaiting = new ArrayList<Picture>();
				orderBoxeNosWaiting = new ArrayList<JLabel>();
				//last lane
				System.out.println("---waiting---");
				locX = 5;
				locY = 170;
				for(int i=0; i<waitingArray.size(); i++){
					orderBox = new Picture(50,50);
					orderBoxNo = new JLabel();
					container.add(orderBox);
					orderBoxesWaiting.add(orderBox);
					container.add(orderBoxNo);
					orderBoxeNosWaiting.add(orderBoxNo);
					orderBoxNo.setForeground(Color.WHITE);
					orderBoxNo.setText(String.valueOf(waitingArray.get(i).getOrderNo()));
					orderBox.set(new ImageIcon("src/images/SmallBox/SmallBox.png"));
					
					orderBox.setLocation(locX, locY);
					orderBoxNo.setBounds(locX, locY+50, 50, 10);
					locX += 50;				

				}
				
			}
			for(int i=0; i<orderBoxesInTransit.size(); i++){
				container.add(orderBoxesInTransit.get(i));
			}
			for(int i=0; i<orderBoxeNosInTransit.size(); i++){
				container.add(orderBoxeNosInTransit.get(i));
			}
			for(int i=0; i<orderBoxesProcessing.size(); i++){
				container.add(orderBoxesProcessing.get(i));
			}
			for(int i=0; i<orderBoxeNosProcessing.size(); i++){
				container.add(orderBoxeNosProcessing.get(i));
			}
			for(int i=0; i<orderBoxesWaiting.size(); i++){
				container.add(orderBoxesWaiting.get(i));
			}
			for(int i=0; i<orderBoxesWaiting.size(); i++){
				container.add(orderBoxesWaiting.get(i));
			}
			previousTransitSize = inTransitArray.size();
			previousProcessingSize = processingArray.size();
			previousWaitingSize = waitingArray.size();
			
			
		} catch (OrderException e) {
			e.printStackTrace();
		}
	  }else{
		  System.out.println("Nope.");
	  }
  }
  

}